<?php 
require_once("perficature-utils.php");
insertHeader("Stats");

$table1 = "<table border=1 cellpadding=2 cellspacing=0>\n" .
	"  <tr> <th>&nbsp;</th> <th>Tests</th> <th>Browsers</th> <th>User-Agents</th> <th>IPs</th> </tr>\n";
$table1 .= table1Row("TOTALS",
					 doSimpleQuery("select count(*) from $gTestsTable;"),
					 doSimpleQuery("select count(distinct(pbrowser)) from $gTestsTable;"),
					 doSimpleQuery("select count(distinct(useragent)) from $gTestsTable;"),
					 doSimpleQuery("select count(distinct(ip)) from $gTestsTable;"),
					 1);
// last N days
$gnDays = 14;
$aNow = getdate();
$nextMidnight = mktime(23, 59, 59, $aNow['mon'], $aNow['mday'], $aNow['year']);
$dailies = "";

for ( $i = 0; $i < $gnDays; $i++ ) {
	$end = $nextMidnight - ($i * 24 * 60 * 60);
	$begin = $nextMidnight - (($i+1) * 24 * 60 * 60);
	$table1 .= table1Row(( $i < 3 ? "<a href='#$end'>" . date("D n/d", $end) . "</a>" : date("D n/d", $end) ),
						 doSimpleQuery("select count(*) from $gTestsTable where createdate > $begin and createdate <= $end;"),
						 doSimpleQuery("select count(distinct(pbrowser)) from $gTestsTable where createdate > $begin and createdate <= $end;"),
						 doSimpleQuery("select count(distinct(useragent)) from $gTestsTable where createdate > $begin and createdate <= $end;"),
						 doSimpleQuery("select count(distinct(ip)) from $gTestsTable where createdate > $begin and createdate <= $end;"),
						 $i);
	if ( $i < 3 ) {
		// only do a few days of details
		$dailies .= doDaily($begin, $end);
	}
}

$table1 .= "</table>\n\n";


function table1Row($label, $tests, $browsers, $useragents, $ips, $iRow) {
	return "  <tr" . ( $iRow % 2 == 0 ? " class=altbg" : "" ) . "> <td align=right style='padding: 0 8px 0 8px;'>$label</td> <td align=right style='padding: 0 8px 0 8px;'>$tests</td> <td align=right style='padding: 0 8px 0 8px;'>$browsers</td> <td align=right style='padding: 0 8px 0 8px;'>$useragents</td> <td align=right style='padding: 0 8px 0 8px;'>$ips</td> </tr>\n";
}


function doDaily($begin, $end) {
	global $gTestsTable;

	$daily = "<a name='$end'></a>\n<h2>" . date("D n/d/y", $end) . "</h2>\n" .
		"<table cellpadding=8 cellspacing=0 border=0>\n" .
		"  <tr> <th>Time</th> <th>IP</th> <th>Browser</th> <th>Results</th> <th>User Agent</th> </tr>\n";

	$query = "select testid, createdate, ip, pbrowser, useragent from $gTestsTable where createdate > $begin and createdate <= $end order by createdate asc;";
	$result = doQuery($query);
	$i = 0;
	while ($row = mysql_fetch_assoc($result)) {
		$i++;
		displayTestResults($row['testid'], $totalscore, $totaltests);
		$daily .= "  <tr" . ( $i % 2 == 0 ? " class=altbg" : "" ) . "> <td class=dailytd>" . date("H:i:s", $row['createdate']) . 
			"</td> <td class=dailytd>" . $row['ip'] . "<br>" . gethostbyaddr($row['ip']) .
			"</td> <td class=dailytd><nobr><b>" . $row['pbrowser'] .
			"</td> <td class=dailytd><b>$totalscore/$totaltests" . 
			"</td> <td class=dailytd>" . $row['useragent'] .
			"</td> </tr>\n";
	}
	mysql_free_result($result);
	$daily .= "</table>\n\n";

	return $daily;
}

?>
<style>
TH { background: #CCC; }
.altbg { background: #E8E8E8; }
.dailytd { border-style: solid; border-color: #CCC; border-width: 1px 0 1px 0; }
</style>

<?php echo $gAdminLinks ?>

</div> <!-- contents -->
<div style="padding: 8px;">
<style>
H3, H4, TABLE, P { margin-left: 8px; margin-right: 8px;}
H3 { margin-bottom: 4px; }
#stats1 TD { border-bottom: 1px solid #AAA; padding: 2px 4px 2px 4px; }
#stats1 TH { border: 1px solid #AAA; padding: 2px 4px 2px 4px; background: #EEE; }
</style>

<?php
echo $table1;
echo $dailies;
?>

</div> <!-- stats1 -->

<?php insertFooter(); ?>
